package bdgrade;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import domain.Stu;
import domain.Student;
import service.Impl.StuServiceImpl;
import tools.FileTool;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.ImageIcon;
import java.awt.Font;

public class Change {
	//修改界面
	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private StuServiceImpl stuService=new StuServiceImpl();
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Change window = new Change(1);
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Change(int row) {
		initialize(row);
	}

	private void initialize(int row) {		
		frame = new JFrame();
		frame.getContentPane().setBackground(Color.BLACK);
		frame.setTitle("修改页面");
		frame.setBounds(100, 100, 505, 415);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setResizable(false);
		frame.setVisible(true);
		
		//通过传递过来的row,获取要修改学生的原信息并显示到输入框
//		Student s=Data.get(row);
		Stu s = stuService.findByRow(row);
		JLabel label = new JLabel("学号");
		label.setFont(new Font("宋体", Font.PLAIN, 18));
		label.setForeground(Color.WHITE);
		label.setBounds(28, 37, 72, 18);
		frame.getContentPane().add(label);
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 18));
		textField.setBounds(87, 34, 208, 24);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		textField.setText(s.getID()+"");
		
		JLabel label_1 = new JLabel("姓名");
		label_1.setFont(new Font("宋体", Font.PLAIN, 18));
		label_1.setForeground(Color.WHITE);
		label_1.setBounds(28, 89, 72, 18);
		frame.getContentPane().add(label_1);
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_1.setBounds(87, 86, 208, 24);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		textField_1.setText(s.getName());
		
		JLabel label_2 = new JLabel("课程");
		label_2.setFont(new Font("宋体", Font.PLAIN, 18));
		label_2.setForeground(Color.WHITE);
		label_2.setBounds(28, 147, 72, 18);
		frame.getContentPane().add(label_2);
		textField_2 = new JTextField();
		textField_2.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_2.setBounds(87, 144, 208, 24);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		textField_2.setText(s.getCourse());
		
		JLabel label_3 = new JLabel("班级");
		label_3.setFont(new Font("宋体", Font.PLAIN, 18));
		label_3.setForeground(Color.WHITE);
		label_3.setBounds(28, 207, 72, 18);
		frame.getContentPane().add(label_3);
		textField_3 = new JTextField();
		textField_3.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_3.setBounds(87, 204, 208, 24);
		frame.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		textField_3.setText(s.getClasses());
		
		JLabel label_4 = new JLabel("成绩");
		label_4.setFont(new Font("宋体", Font.PLAIN, 18));
		label_4.setForeground(Color.WHITE);
		label_4.setBounds(28, 267, 72, 18);
		frame.getContentPane().add(label_4);
		textField_4 = new JTextField();
		textField_4.setFont(new Font("宋体", Font.PLAIN, 18));
		textField_4.setBounds(87, 264, 208, 24);
		frame.getContentPane().add(textField_4);
		textField_4.setColumns(10);
		textField_4.setText(s.getScore());
		
		//修改按钮
		JButton button = new JButton("确定修改");
		button.setFont(new Font("宋体", Font.PLAIN, 15));
		button.setForeground(Color.WHITE);
		button.setBackground(Color.BLACK);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//确定修改
				String id=textField.getText();
				String name=textField_1.getText();
				String course=textField_2.getText();
				String classes=textField_3.getText();
				String score=textField_4.getText();
				System.out.println("----------------------"+id);
				Stu stu=new Stu();
				stu.setID(Integer.parseInt(id));
				stu.setName(name);stu.setCourse(course);stu.setClasses(classes);stu.setScore(score);
				stuService.update(stu);
				frame.dispose();
			}
		});
		button.setBounds(355, 327, 113, 28);
		frame.getContentPane().add(button);
		
		JButton btnNewButton = new JButton("返回");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 15));
		btnNewButton.setForeground(Color.WHITE);
		btnNewButton.setBackground(Color.BLACK);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				frame.dispose();
			}
		});
		btnNewButton.setBounds(228, 327, 113, 28);
		frame.getContentPane().add(btnNewButton);
		
		JLabel label_5 = new JLabel("");
		label_5.setIcon(new ImageIcon(Change.class.getResource("/img/1.jpg")));
		label_5.setBounds(114, 52, 406, 262);
		frame.getContentPane().add(label_5);	
	}
}
